/*
 * CAppStateManager.cpp
 *
 *  Created on: 24/12/2012
 *      Author: Dalton
 */

#include "CAppStateManager.h"
// Refer to your Other App States Here
#include "CAppStateIntro.h"
#include "CAppStateMenu.h"
#include "CAppStateCreateGame.h"
#include "CAppStatePlayGame.h"
#include "CAppStateGame.h"
#include "CAppStateStage.h"
#include <iostream>

using namespace std;

CAppState* CAppStateManager::activeAppState = 0;

void CAppStateManager::onEvent(SDL_Event* eventHolder) {
    if(activeAppState) activeAppState->onEvent(eventHolder);
}

void CAppStateManager::onLoop() {
    if(activeAppState) activeAppState->onLoop();
}

void CAppStateManager::onRender(SDL_Surface* surf_Display) {
    if(activeAppState) activeAppState->onRender(surf_Display);
}

void CAppStateManager::setActiveAppState(int appStateID) {
	CAppState* stateAux = activeAppState;

    // Also, add your App State Here so that the Manager can switch to it
    switch(appStateID){
    	case APPSTATE_NONE:
    		activeAppState = 0;
    		break;
    	case APPSTATE_INTRO:
    		activeAppState = CAppStateIntro::getInstance();
    		break;
    	case APPSTATE_MENU:
    		 activeAppState = CAppStateMenu::getInstance();
    		 break;
    	case APPSTATE_CREATE:
    		 activeAppState = CAppStateCreateGame::getInstance();
    		 break;
    	case APPSTATE_PLAY:
    		activeAppState = CAppStatePlayGame::getInstance();
    		break;
    	case APPSTATE_GAME:
    		activeAppState = CAppStateGame::getInstance();
    		break;
    	case APPSTATE_STAGE_CLIENT:
    		CAppStatePlayGame* aux_client;
    		aux_client = (CAppStatePlayGame*) stateAux;
    		CAppStateStage* stage_client;
    		stage_client = CAppStateStage::getInstance();
    		stage_client->setTcpClient(aux_client->getTcpClient());
    		activeAppState = stage_client;
    		break;
    	case APPSTATE_STAGE_SERVER:
    		CAppStateCreateGame* aux;
    		aux = (CAppStateCreateGame*) stateAux;
    		CAppStateStage* stage;
    		stage = CAppStateStage::getInstance();
    		stage->setTcpListener(aux->getTcpListener());
    		activeAppState = stage;
    		break;
    	default:
    		//por msg de erro
    		break;

    }

    if(stateAux) stateAux->onDeactivate();
    if(activeAppState) activeAppState->onActivate();
}

CAppState* CAppStateManager::getActiveAppState() {
    return activeAppState;
}


